package com.itheima.demo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

@WebServlet(urlPatterns = "/servletDemo1")
public class ServletDemo1 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //根据名称获取数据
        String username = request.getParameter("username");
        System.out.println(username);
        String password = request.getParameter("password");
        System.out.println(password);
        System.out.println("----------------------------------");
        //根据名称获取所有数据
        String[] parameterValues = request.getParameterValues("hobby");
        for (String s : parameterValues) {
            System.out.println(s);
            System.out.println("----------------------------------------");
        }

        //获取所有名称
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()){
            String s = names.nextElement();
            System.out.println(s);
            System.out.println("----------------------------------------");
        }
        //获取所有的键值对
        Map<String, String[]> map = request.getParameterMap();
        for (String key : map.keySet()) {
            String[] values = map.get(key);
            for (String value : values) {
                System.out.println(key+":"+value);
            }
        }


    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}